package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.Login;
import where2go.sctu.com.zhiyou.entity.NewUser;
import where2go.sctu.com.zhiyou.entity.User;
import where2go.sctu.com.zhiyou.utils.HeaderStringRequest;
import where2go.sctu.com.zhiyou.utils.ShareUtils;

/**
 * Created by lenovo on 2018/5/10.
 */

public class PersonalImformationActivity extends AppCompatActivity {
    ImageView imageView;
    TextView textView;

    private TextView userNameTv;
    private TextView nickNameTv;
    private TextView sexTv;
    private TextView ageTv;
    private TextView emailTv;
    private TextView userDetailTv;
    private ImageView imageTv;

    private NewUser.DataBean user=new NewUser.DataBean();

    private NewUser users= new NewUser();
    private String url1 = "http://47.104.80.155/youyiyou/resources/img/";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_imformation);
        initView();
        getUserInformation();
        textView=(TextView)findViewById(R.id.textView0) ;
        imageView=(ImageView) findViewById(R.id.pernal);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent8=new Intent();
                intent8.setClass(getApplication(),ModifyInformationActivity.class);
                startActivity(intent8);
            }
        });
//        Log.d("iamge",user.getUserAvatarAddress());
    }


    private void initView() {

        userNameTv=(TextView)findViewById(R.id.username);
        nickNameTv=(TextView)findViewById(R.id.nickname);
        sexTv=(TextView)findViewById(R.id.gender);
        ageTv=(TextView)findViewById(R.id.age);
        emailTv=(TextView)findViewById(R.id.email);
        userDetailTv=(TextView)findViewById(R.id.user_detail);
        imageTv=(ImageView)findViewById(R.id.image_Tv);
    }

    private void getUserInformation(){
        final String url="http://47.104.80.155/youyiyou/users/details/show";
       HeaderStringRequest request=new HeaderStringRequest(Request.Method.GET, url, new Response.Listener<String>() {
           @Override
           public void onResponse(String response) {
               Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
               Type type = new TypeToken<NewUser>() {
               }.getType();
              users=gson.fromJson(response,type);
              user=users.getData();
              userNameTv.setText(user.getUserName());
              nickNameTv.setText(user.getUserNickName());
                ageTv.setText(String.valueOf(user.getUserAge()));
              if (user.getUserSex()==1){
                  sexTv.setText("女");
              }else {sexTv.setText("男");}
              emailTv.setText(user.getUserEmail());
              userDetailTv.setText(user.getUserDetail());


               ImageRequest imageRequest = new ImageRequest(url1 + user.getUserAvatarAddress(),
                       new Response.Listener<Bitmap>() {
                           @Override
                           public void onResponse(Bitmap bitmap) {
                               imageTv.setImageBitmap(bitmap);
                           }
                       }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                   @Override
                   public void onErrorResponse(VolleyError error) {
                       //请求失败 设置默认图片
                       imageTv.setImageResource(R.mipmap.ic_launcher);
                   }

               });
               MyApplication.getHttpQueues().add(imageRequest);

           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {

           }
       }){
           @Override
           public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String,String> map=new HashMap<String ,String >();
               map.put("authorization", ShareUtils.getString(PersonalImformationActivity.this,"token",null));
               return map;
           }
       };
       MyApplication.getHttpQueues().add(request);
   }

}
